package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PreDestroy;
import java.util.List;
import java.util.Map;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;
    //添加购物车
    @PostMapping("/cart/add")
    public ResultInfo add(@RequestBody Cart cartParam) { // 接收参数
        //2.调用service
        Cart cart = cartService.add(cartParam);
        //3.返回结果
        return ResultInfo.success(cart);
    }

    //查询购物车
    @GetMapping("/cart/list")
    public ResultInfo list() {
        // 1.调用service
        //List<Cart> cartList = cartService.list();
        // 2.返回结果
        //return ResultInfo.success(cartList);
        // 链式编程
        return ResultInfo.success(cartService.list());

    }

    //修改购物车
    @PostMapping("/cart/sub")
    public ResultInfo updateById(@RequestBody Map<String,Long> mapPam) {
        // 2.调用service
        Long dishId = mapPam.get("dishId");
        Long setmealId = mapPam.get("setmealId");
        Cart cart = cartService.updateCart(dishId,setmealId);
        //3.返回结果
        return ResultInfo.success(cart);
    }

    //清空购物车
    @DeleteMapping("/cart/clean")
    public ResultInfo clean() {
        //1.调用service清空
        cartService.clean();
        //2.返回结果
        return ResultInfo.success();
    }


}
